package com.moco.launcher.activity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moco.launcher.R;
import com.moco.launcher.UpdateReceiver;
import com.oazon.common.Logger;
import com.oazon.common.Settings;
import com.oazon.moco.link.bean.BatteryInfo;
import com.oazon.moco.link.bean.Mcu;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.ui.UpdateEventListener;
import com.oazon.moco.serialport.SerialControll;
import com.oazon.moco.serialport.SerialHandlerService;
import com.oazon.moco.serialport.SerialSend;
import com.oazon.system.SysActionChannal;
import com.oazon.voicelib.service.SpeechManage;

import org.apache.commons.lang3.StringUtils;

import java.util.Set;

public class UpdateMsgActivity extends BaseActivity implements OnClickListener {


    private Button mBtnConfirm;
    private ImageView mImgViewBack;
    private CheckBox isupdate, isaggree;

    private final String TAG = "UpdateMsgActivity";

    private EventListener mEventListener = new EventListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("UpdateMsgActivity", "==oncreate");

        setContentView(R.layout.update_msg_activity);
        isupdate = (CheckBox) findViewById(R.id.update_timer);
        isaggree = (CheckBox) findViewById(R.id.atircle);
        mBtnConfirm = (Button) findViewById(R.id.confirm);
        mBtnConfirm.setOnClickListener(this);

        mImgViewBack = (ImageView) findViewById(R.id.id_back);
        mImgViewBack.setOnClickListener(this);

        isupdate.setChecked(Settings.getInstance(UpdateMsgActivity.this).get("update").equals("true"));

        //isaggree.setChecked(Config.getInstance(UpdateMsgActivity.this).get("article").equals("true") ? true : false);
        setBetaCheckbox();

        isupdate.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (arg1) {
                    Settings.getInstance(UpdateMsgActivity.this).set("update", "true");
                } else {
                    Settings.getInstance(UpdateMsgActivity.this).set("update", "false");
                }
            }
        });
        isaggree.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (arg1) {
                    //ShowConnectDialog("       O2欢迎客户试用暂未发布的测试版软件，测试新功能并帮助我们改善产品。在您运行测试版软件时，部分功能可能会不正常。是否开启？");

                    Intent i = new Intent(UpdateMsgActivity.this, BetaTestActivity.class);
                    UpdateMsgActivity.this.startActivity(i);
                } else {
                    Settings.getInstance(UpdateMsgActivity.this).set("article", "false");
                }
            }
        });
      LinkSDK.getInstance().registLinkListener(mEventListener);

    }

    public class EventListener extends UpdateEventListener {

        @Override
        protected void beforeUpdate() {
            super.beforeUpdate();
            Log.e(TAG, "beforeUpdate");
            SerialControll.getInstance().stopControl();
            SerialHandlerService.stopWatchDog();
            SpeechManage.getInstance().stopWake();
            SysActionChannal.getInstance().setPauseAllTimer(true);
        }

        @Override
        protected void afterUpdate() {
            super.afterUpdate();
            Logger.d(TAG, "afterUpdate");
            SerialControll.getInstance().startControl();
            SerialHandlerService.startWatchDog();
            SpeechManage.getInstance().startWake();
            SysActionChannal.getInstance().setPauseAllTimer(false);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        setBetaCheckbox();
    }

    private void setBetaCheckbox() {
        if(isaggree == null)
            return;
        if(Settings.getInstance(this).get("article").equals("true")) {
            isaggree.setChecked(true);
        } else {
            isaggree.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mBtnConfirm.getId()) {
            start();
        }

        if (v.getId() == mImgViewBack.getId()) {
            finish();
        }
    }
    public void start() {
  //--gavin---如果升级失败，请打开这里的注释测试是否有音响，发现up已经发送过同意的广播关闭串口，导致进入升级界面返回后，单片机所有通信中断-----------	
/*        Intent i = new Intent();
        i.setAction(UpdateReceiver.ACTION_UPDATE_START);
        sendBroadcast(i);*/

        ComponentName componetName = new ComponentName(
                //这个是另外一个应用程序的包名
                "com.example.up",
                //这个参数是要启动的Activity
                "com.example.up.MainActivity");
        Intent intent = new Intent();
        intent.setComponent(componetName);
        if(Mcu.mcu_verison!=null)
           intent.putExtra("mcu_version", StringUtils.trim(Mcu.mcu_verison));
        else
           intent.putExtra("mcu_version", "01 01");
        intent.putExtra("batter_percent", BatteryInfo.ChargePercent);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LinkSDK.getInstance().unregistLinkListener(mEventListener);
    }

}
